package com.hpeng.daily.action;


import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;


import com.hpeng.daily.service.AssetCardBackUpManage;
import com.hpeng.daily.service.AssetCardManager;
import com.hpeng.sys.model.AssetCard;
import com.hpeng.sys.model.AssetCardBackUp;
import com.hpeng.sys.model.Category;
import com.hpeng.sys.model.Department;
import com.hpeng.sys.service.Function;
import com.hpeng.sys.service.LoginUserMgr;
import com.hpeng.sys.vo.Lookup;
import com.hpeng.util.Pagination;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Component("assetCardCSchangeAction")
@Scope("prototype")
public class AssetCardCSchangeAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	// 用于分页显示
	private int page;
	public String pageLink;
	private Pagination<AssetCard> pagination;
	
	@Resource
	private AssetCardManager assetCardManager;
	@Resource
	private AssetCardBackUpManage assetCardBackUpManage;
	private AssetCardBackUp acbu;
	@Resource
	private LoginUserMgr loginUser;
	private List<Department> departments;
	private AssetCard assetCard;
	private String idArray; // 获取被选择的Id编号数组 从页面上传到后台的数组
	private String msg;
	private String catid; // 类别编号
	private String acid; // 资产的编号
	private String result;// 处理意见
	private List<Lookup> ls;// 用于页面上显示资产的状态的list
	// 用于根据权限生成功能按钮
	@Resource
	private Function function;
	private String accessRight;
	private String FN;
	// 条件查询
	private String condition; // 查询条件
	private String conditionValue;// 输入的查询值
	private String acstatusValue;

	// 用于分页查询 显示资产卡片参数
	
	public String searchAssetCardCSchange() {
		String order = " ";
			String[] hql = { " from AssetCard " };
			setPagination(assetCardManager.getPagination(page, hql, order));
		pageLink = pagination.getPageLink("searchAssetCardCSchange.action?FN=" + FN);
		this.accessRight = function.getAccessRight(session(), FN);
		ls = function.getLookup("prod_status_mst", "status_code||sub_code",
				"status_desc||' - '||sub_desc");
		return "success";
	}
	

	// 按照条件进行查询
	public String queryAssetCard() {

		try {
			if(conditionValue!=null){
			       conditionValue = new String(conditionValue.getBytes("ISO8859-1"),
					"UTF-8");
			       }
			String append = "";
			if ("zclb".equals(condition)) {
				
				append = "a.category.catname like ('%"+conditionValue+"%') ";
//				System.out.println("----------------------------");
//				System.out.println(condition);
//				System.out.println(conditionValue);
//				System.out.println("----------------------------");
				
				String[] hql = {" from AssetCard a where ",append };
				
				String order = " order by a.acid asc";
				
				setPagination(assetCardManager.getPagination(page, hql, order));
				
				
			} else if ("zczt".equals(condition)) {
				
				append = " and acstatus like ('%"+acstatusValue+"%') ";
				
				String[] hql = {" from AssetCard a where a.acstatus like '"+ acstatusValue.subSequence(0, 1) + "%' ",append };
				String order = " order by a.acid asc";
				setPagination(assetCardManager.getPagination(page, hql, order));
				
			} else if ("sbbm".equals(condition)) {
				
				append = " reportdepartment like ('%"+conditionValue+"%') ";
				
				String[] hql = {" from AssetCard a where",append };
				
				String order = " order by a.acid asc";
				
				setPagination(assetCardManager.getPagination(page, hql, order));
			}
			
//			System.out.println("----------------------------------");
//			System.out.println("hql[0]"+hql[0]);
//			System.out.println("hql[1]"+hql[1]);
//			System.out.println(acstatusValue.subSequence(0, 1));
//			System.out.println("----------------------------------");
			
			
			
			
			pageLink = pagination
					.getPageLink("queryAssetCard.action?condition=" + condition
							+ "&conditionValue=" + conditionValue+"&acstatusValue=" + acstatusValue);
			this.accessRight = function.getAccessRight(session(), FN);
			ls = function.getLookup("prod_status_mst", "status_code||sub_code",
					"status_desc||' - '||sub_desc");
		} catch (UnsupportedEncodingException e) {
			System.out.println(e.getMessage());
		}
		return "success";
	}

	

	// 点击类别树上的类别得到该类别下的资产
	public String queryAssetByCategory() {
		String[] hql = { " from AssetCard where catid like '"+catid+"%'"};
		String order = " ";
		setPagination(assetCardManager.getPagination(page, hql, order));
		pageLink = pagination.getPageLink("getAssetByCategory.action?FN=" + FN+"&catid="+catid);
		this.accessRight = function.getAccessRight(session(), FN);
		ls = function.getLookup("prod_status_mst", "status_code||sub_code",
				"status_desc||' - '||sub_desc");
		return "success";
	}


	/**
	 * 
	 * 修改卡片参数
	 * @throws ParseException 
	 */
	public String updateAssetCard() throws ParseException {
		try {
			Category c = new Category();
			c.setCatid(catid);
			assetCard.setCategory(c);
//			HttpServletRequest request=ServletActionContext.getRequest();
			
			String username=loginUser.getUserName(request());
			acbu=this.getAssetCardBack(assetCard);
			acbu.setChangepeople(username);
			acbu.setChangedate(new Date());
			System.out.println("------------------"+new Date().toString()+"----------------------");
			System.out.println(username);

			if (assetCardBackUpManage.addAssetCardBackUp(acbu)) {
				assetCardManager.updateAssetCard(assetCard, "E", loginUser
						.getUserId(request()), null,function.getStatusDesc("1", "10"));
			}else {
				msg=getText("信息录入失败！");
			}
			   

		} catch (RuntimeException rx) {
			rx.printStackTrace();
			msg = getText("update.failure");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("update.success");
		}
		this.accessRight = function.getAccessRight(session(), FN);
		return searchAssetCardCSchange();
	}
	
	public AssetCardBackUp getAssetCardBack(AssetCard assetCard){
		acbu=new AssetCardBackUp();
		acbu.setAcid(assetCard.getAcid());
		acbu.setAccode(assetCard.getAccode());
		acbu.setAcname(assetCard.getAcname());
		acbu.setAcanothername(assetCard.getAcanothername());
		acbu.setAcstatus(assetCard.getAcstatus());
		acbu.setAssetcomefrom(assetCard.getAssetcomefrom());
		acbu.setReportreason(assetCard.getReportreason());
		acbu.setReportdepartment(assetCard.getReportdepartment());
		acbu.setReportprice(assetCard.getReportprice());
		acbu.setProducedate(assetCard.getProducedate());
		acbu.setProducer(assetCard.getProducer());
		acbu.setPlanstopdate(assetCard.getPlanstopdate());
		acbu.setActualstopdate(assetCard.getActualstopdate());
		acbu.setDepname(assetCard.getDepname());
		acbu.setIspublic(assetCard.getIspublic());
		acbu.setAcuser(assetCard.getAcuser());
		acbu.setOrderid(assetCard.getOrderid());
		acbu.setOrderprice(assetCard.getOrderprice());
		acbu.setAccountsubject(assetCard.getAccountsubject());
		acbu.setScrapmanage(assetCard.getScrapmanage());
		acbu.setScrapworth(assetCard.getScrapworth());
		acbu.setLifetime(assetCard.getLifetime());
		acbu.setCount(assetCard.getCount());
		acbu.setIsscrap(assetCard.getIsscrap());
		acbu.setIsloss(assetCard.getIsloss());
		acbu.setExtendnumberone(assetCard.getExtendnumberone());
		acbu.setExtendnumbertwo(assetCard.getExtendnumbertwo());
		acbu.setExtendnumberthree(assetCard.getExtendnumberthree());
		acbu.setExtendcharone(assetCard.getExtendcharone());
		acbu.setExtendchartwo(assetCard.getExtendchartwo());
		acbu.setExtendcharthree(assetCard.getExtendcharthree());
		acbu.setExtendcharfour(assetCard.getExtendcharfour());
		acbu.setExtenddateone(assetCard.getExtenddateone());
		acbu.setExtenddatetwo(assetCard.getExtenddatetwo());
		acbu.setExtenddatethree(assetCard.getExtenddatethree());
		
		return acbu;
	}
	
	
	/********************** set get方法 ***********************************/
	public void setAssetCardManager(AssetCardManager assetCardManager) {
		this.assetCardManager = assetCardManager;
	}

	public AssetCard getAssetCard() {
		return assetCard;
	}

	public void setAssetCard(AssetCard assetCard) {
		this.assetCard = assetCard;
	}

	public Pagination<AssetCard> getPagination() {
		return pagination;
	}

	public void setPagination(Pagination<AssetCard> pagination) {
		this.pagination = pagination;
	}

	public void setAssetCardManage(AssetCardManager assetCardManager) {
		this.assetCardManager = assetCardManager;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPage() {
		return page;
	}

	public void setIdArray(String idArray) {
		this.idArray = idArray;
	}

	public String getIdArray() {
		return idArray;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getCatid() {
		return catid;
	}

	public void setCatid(String catid) {
		this.catid = catid;
	}

	public String getAccessRight() {
		return accessRight;
	}

	public void setAccessRight(String accessRight) {
		this.accessRight = accessRight;
	}

	public String getFN() {
		return FN;
	}

	public void setFN(String fN) {
		FN = fN;
	}

	public HttpServletRequest request() {
		return ServletActionContext.getRequest();
	}

	public Map<String, Object> session() {
		return ActionContext.getContext().getSession();
	}

	public String getAcid() {
		return acid;
	}

	public void setAcid(String acid) {
		this.acid = acid;
	}

	public List<Lookup> getLs() {
		return ls;
	}

	public void setLs(List<Lookup> ls) {
		this.ls = ls;
	}

	public List<Department> getDepartments() {
		return departments;
	}

	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	public String getConditionValue() {
		return conditionValue;
	}

	public void setConditionValue(String conditionValue) {
		this.conditionValue = conditionValue;
	}
	public String getAcstatusValue() {
		return acstatusValue;
	}

	public void setAcstatusValue(String acstatusValue) {
		this.acstatusValue = acstatusValue;
	}

}
